package com.yztstudy.Service.Impl;

import com.yztstudy.Service.AdminProductService;
import com.yztstudy.clients.ProductFeignClient;
import com.yztstudy.clients.SearchFeignClient;
import com.yztstudy.entity.Product;
import com.yztstudy.param.ProductSaveParam;
import com.yztstudy.param.ProductSearchParam;
import com.yztstudy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class AdminProductServiceImpl implements AdminProductService {

    @Resource
    private SearchFeignClient searchFeignClient;

    @Resource
    private ProductFeignClient productFeignClient;

    /**
     * TODO 商品的展示与搜索
     * @param productSearchParam 前端传过来的搜索参数
     * @return
     */
    @Override
    public R productSearch(ProductSearchParam productSearchParam) {

        // TODO 1.远程调用搜索功能
        R result = searchFeignClient.searchProduct(productSearchParam);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.productSearch方法执行完毕,结果为:{}", "商品展示成功!");
        }else {
            log.info("AdminUserServiceImpl.productSearch方法执行完毕,结果为:{}", "商品展示失败!");
        }
        return result;
    }


    /**
     * TODO 保存商品
     * @param productSaveParam 商品
     * @return
     */
    @Override
    public R productSave(ProductSaveParam productSaveParam) {

        // TODO 1.远程调用保存功能
        R result = productFeignClient.adminSave(productSaveParam);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.productSave方法执行完毕,结果为:{}", "商品保存成功!");
        }else {
            log.info("AdminUserServiceImpl.productSave方法执行完毕,结果为:{}", "商品保存失败!");
        }
        return result;
    }


    /**
     * TODO 更新商品
     * @param product 商品
     * @return
     */
    @Override
    public R productUpdate(Product product) {

        // TODO 1.远程调用商品服务
        R result = productFeignClient.adminUpdate(product);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.productUpdate方法执行完毕,结果为:{}", "商品更新成功!");
        }else {
            log.info("AdminUserServiceImpl.productUpdate方法执行完毕,结果为:{}", "商品更新失败!");
        }
        return result;
    }


    /**
     * TODO 删除商品
     * @param productId 商品ID
     * @return
     */
    @Override
    public R productRemove(Integer productId) {

        // TODO 1.调用远程服务
        R result = productFeignClient.adminRemove(productId);

        // TODO 2.日志记录
        if (result.getCode().equals(R.SUCCESS_CODE))
        {
            log.info("AdminUserServiceImpl.productRemove方法执行完毕,结果为:{}", "商品删除成功!");
        }else {
            log.info("AdminUserServiceImpl.productRemove方法执行完毕,结果为:{}", "商品删除失败!");
        }
        return result;
    }
}
